<style>
.text {
  font-size: 16px;
}

.clearfix{
  background-color: #F9F9F9;
  color: #666666;
  font-weight: bold;
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both
}
.el-card__header{
  background-color: #F9F9F9;
}
.box-card {
  width: 100%;
  margin: auto;
  margin-bottom: 10px;
}
.foot{
  box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
  position: fixed;
  bottom: 0;
  background-color: #fff;
  width: calc(100vw - 200px);
  right: 0;
}
.app-container{
  position: relative;
}

.el-table__header-wrapper .el-checkbox {
  display: none;
}
.trees{
  height: 200px;
  overflow: auto;
}
</style>
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺名称" prop="mallName">
        <el-input
          v-model="queryParams.mallName"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:mall:add']"
        >新增授权</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:mall:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:mall:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="mallList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column show-overflow-tooltip label="店铺名称" align="center" prop="mallName" />
      <el-table-column show-overflow-tooltip label="店铺ID" align="center" prop="mallId" />
      <el-table-column show-overflow-tooltip label="店铺描述" align="center" prop="mallDesc" />
      <el-table-column label="logo" align="center" prop="logo" width="80">
        <template slot-scope="scope">
          <ImagePreview
            :width=50 :height=50
            :src="scope.row.logo"
            :preview-src-list="[scope.row.logo]"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="店铺类型" align="center" prop="merchantType" />-->
<!--      <el-table-column label="店铺身份" align="center" prop="mallCharacter" />-->
      <el-table-column label="店铺设置" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:mall:edit']"
          >设置</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:mall:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改店铺对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>监控设置</span>
          </div>
          <div class="text item">
            <el-row>
              <el-col :span="12">
                <el-form-item label="盈利预警" prop="yingliyujing">
                  <el-input v-model="form.yingliyujing" placeholder="请输入盈利预警" type="number"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="优惠券" prop="youhuiquan">
                  <el-input v-model="form.youhuiquan" placeholder="请输入优惠券" type="number"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="联盟下架是否下架" prop="xiajia">
                  <el-switch
                    v-model="form.xiajia"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    active-value="1"
                    inactive-value="0">
                  </el-switch>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="专项链接是否下架" prop="zhuanxiang">
                  <el-switch
                    v-model="form.zhuanxiang"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    active-value="1"
                    inactive-value="0">
                  </el-switch>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="无库存是否下架" prop="kucun">
                  <el-switch
                    v-model="form.kucun"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    active-value="1"
                    inactive-value="0">
                  </el-switch>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="盈利预警是否下架" prop="yingli">
                  <el-switch
                    v-model="form.yingli"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    active-value="1"
                    inactive-value="0">
                  </el-switch>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="发货超过48小时是否下架" prop="fahuo">
                  <el-switch
                    v-model="form.fahuo"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    active-value="1"
                    inactive-value="0">
                  </el-switch>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="不包邮是否下架" prop="baoyou">
                  <el-switch
                    v-model="form.baoyou"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    active-value="1"
                    inactive-value="0">
                  </el-switch>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </el-card>
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>上传设置</span>
          </div>
          <div class="text item">
            <el-row>
              <el-col :span="12">
                <el-form-item label="运费模板" prop="yunfeimoban">
                  <el-select v-model="form.yunfeimoban" style="width: 100%;" placeholder="请选择运费模板">
                    <el-option
                      v-for="item in templateList"
                      :key="item.templateId"
                      :label="item.templateName"
                      :value="item.templateId"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="假一赔十" prop="jiayipeishi">
                  <el-radio v-model="form.jiayipeishi" :label="1">是</el-radio>
                  <el-radio v-model="form.jiayipeishi" :label="0">否</el-radio>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="7天无理由退换货" prop="qitian">
                  <el-radio v-model="form.qitian" :label="1">是</el-radio>
                  <el-radio v-model="form.qitian" :label="0">否</el-radio>
                </el-form-item>
              </el-col>
<!--              <el-col :span="12">-->
<!--                <el-form-item label="类目" prop="">-->
<!--&lt;!&ndash;                  <treeselect  :options="deptOptions" :show-count="true" placeholder="请选择类目" />&ndash;&gt;-->
<!--                  <el-tree-->
<!--                    class="trees"-->
<!--                    :props="props"-->
<!--                    :load="loadNode"-->
<!--                    lazy>-->
<!--                  </el-tree>-->
<!--                </el-form-item>-->
<!--              </el-col>-->
            </el-row>
          </div>
        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMall, getMall, delMall, addMall, updateMall,findCats } from "@/api/system/mall";
import Template from "@/views/system/pddtemplate/index";
import {listTemplateAll} from "@/api/system/pddyftemplate";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Mall",
  components: {Template,Treeselect},
  dicts: ['mall_character','shop_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 店铺表格数据
      mallList: [],
      templateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      deptOptions: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        logo: null,
        mallDesc: null,
        mallId: null,
        mallName: null,
        merchantType: null,
        mallCharacter: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      props:{
        label: 'catName',
        id:'catId',
        parenId:'parentCatId'
      }
    };
  },
  created() {
    this.getList();
    // this.findPddCats();
  },
  methods: {
    /** 查询店铺列表 */
    getList() {
      this.loading = true;
      listMall(this.queryParams).then(response => {
        this.mallList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        logo: null,
        mallDesc: null,
        mallId: null,
        mallName: null,
        merchantType: null,
        mallCharacter: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加店铺";
      const url = "http://27.128.175.119/prod-api/pdd/pddCallback";
      const encodedUrl = encodeURIComponent(url);
      window.open("https://fuwu.pinduoduo.com/service-market/auth?response_type=code&client_id=2ba77f6df16945b6860b9fbb3b9cf8ad&redirect_uri="+encodedUrl+"&state=1", "_blank");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMall(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改店铺";
      });
      //获取运费模板

      listTemplateAll({mallId:row.mallId}).then(data => {
        this.templateList = data
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMall(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMall(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除店铺编号为"' + ids + '"的数据项？').then(function() {
        return delMall(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/mall/export', {
        ...this.queryParams
      }, `mall_${new Date().getTime()}.xlsx`)
    },
    findPddCats(){
      findCats(0).then(data => {
        this.deptOptions = data.data
      })
    },
    loadNode(node, resolve){
      if (node.level === 0) {
        return resolve(this.deptOptions);
      }else {
        findCats(node.catId).then(data => {
          return resolve(data.data);
        })
      }
    }
  }
};
</script>
